#include "napi/native_api.h"
#include "test_pre_export.h"
#include "example_pre_export.h"

struct CallbackData {
    napi_async_work async_work = nullptr;
    napi_deferred deferred = nullptr;
    napi_ref callback = nullptr;
    NativeResourceManager *mgr = nullptr;
    char result[1024] = "";
};

static void ExecuteCB(napi_env env, void *data)
{
    CallbackData *callbackData = reinterpret_cast<CallbackData *>(data);
    auto test = PreExportTest(callbackData->mgr);
    std::string res = test.testAllCase().c_str();
    strcpy(callbackData->result, res.c_str());
}

static void ExampleExecuteCB(napi_env env, void *data)
{
    CallbackData *callbackData = reinterpret_cast<CallbackData *>(data);
    auto example = ExampleExportTest(callbackData->mgr);
    std::string res = example.testAllCase().c_str();
    strcpy(callbackData->result, res.c_str());
}


static void CompleteCB(napi_env env, napi_status status, void *data)
{
    CallbackData *callbackData = reinterpret_cast<CallbackData *>(data);
    napi_value result = nullptr;
    napi_create_string_utf8(env, callbackData->result, 1024, &result);
    napi_resolve_deferred(env, callbackData->deferred, result);
    napi_delete_async_work(env, callbackData->async_work);
    delete callbackData;
    callbackData = nullptr;
}

static napi_value Test(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value argv[1];
    napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);

    NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);

    napi_value promise = nullptr;
    napi_deferred deferred = nullptr;
    napi_create_promise(env, &deferred, &promise);
    auto callbackData = new CallbackData();
    callbackData->deferred = deferred;
    callbackData->mgr = mNativeResMgr;
    napi_value resourceName = nullptr;
    napi_create_string_utf8(env, "Test", NAPI_AUTO_LENGTH, &resourceName);
    napi_create_async_work(env, nullptr, resourceName, ExecuteCB, CompleteCB, callbackData, &callbackData->async_work);
    napi_queue_async_work(env, callbackData->async_work);
    return promise;
}

static napi_value Example(napi_env env, napi_callback_info info)
{
    size_t argc = 1;
    napi_value argv[1];
    napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);

    NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);

    napi_value promise = nullptr;
    napi_deferred deferred = nullptr;
    napi_create_promise(env, &deferred, &promise);
    auto callbackData = new CallbackData();
    callbackData->deferred = deferred;
    callbackData->mgr = mNativeResMgr;
    napi_value resourceName = nullptr;
    napi_create_string_utf8(env, "Example", NAPI_AUTO_LENGTH, &resourceName);
    napi_create_async_work(env, nullptr, resourceName, ExampleExecuteCB, CompleteCB, callbackData, &callbackData->async_work);
    napi_queue_async_work(env, callbackData->async_work);
    return promise;
}


EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
    napi_property_descriptor desc[] = {
        { "test", nullptr, Test, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "example", nullptr, Example, nullptr, nullptr, nullptr, napi_default, nullptr }
    };
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "entry",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
    napi_module_register(&demoModule);
}
